package com.example.h5server.controller;

import com.example.h5server.model.Enterprise;
import com.example.h5server.service.IEnterpriseService;
import com.example.h5server.service.impl.LoginAuthServiceImpl;
import com.example.h5server.utils.Result;
import io.swagger.v3.oas.annotations.Operation;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 企业信息基础信息 前端控制器
 * </p>
 *
 * @author 纸鸽
 * @since 2025-10-23
 */
@RestController
@RequestMapping("/enterprise")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Tag(name = "企业信息接口")
public class EnterpriseController {
    private static final Logger logger = LoggerFactory.getLogger(EnterpriseController.class);
    @Autowired
    private IEnterpriseService enterpriseService;


    //根据企业id查询企业信息
    @GetMapping("/getEnterpriseById")
    @Operation(summary = "根据企业id查询企业信息")
    public Result<Enterprise> getEnterpriseById(Long id) {
        Enterprise enterprise = enterpriseService.getById(id);
        if (enterprise != null) {
            return Result.success("查询成功", enterprise);
        } else {
            return Result.error("企业信息不存在");
        }
    }
    // 修改企业信息（组织架构/成员信息）
    @PostMapping("/updateEnterprise")
    @Operation(summary = "修改企业信息（组织架构/成员信息）")
    public Result<Enterprise> updateOrgJson(@RequestBody Enterprise enterprise) {
        // 检查参数
        if (enterprise.getId() == null) {
            return Result.error("企业ID不能为空");
        }
        // 先根据id查询企业信息
        Enterprise existingEnterprise = enterpriseService.getById(enterprise.getId());
        if (existingEnterprise == null) {
            return Result.error("企业信息不存在");
        }

        // 修改orgJson字段
        existingEnterprise.setOrgJson(enterprise.getOrgJson());

        // 更新数据库
        boolean updated = enterpriseService.updateById(enterprise);
        if (updated) {
            return Result.success("数据更新成功",enterprise);
        } else {
            return Result.error("数据更新失败");
        }
    }
    

    //新增企业信息
    @PostMapping("/addEnterprise")
    @Operation(summary = "新增企业信息")
    public Result<Enterprise> addEnterprise(@RequestBody Enterprise enterprise) {
        if (enterprise == null) {
            return Result.error("企业信息不能为空");
        }
        boolean result = enterpriseService.saveEnterprise(enterprise);
        return result ? Result.success("新增成功",enterprise) : Result.error("新增失败");
    }
    //根据id删除企业信息
    @PostMapping("/deleteEnterprise")
    @Operation(summary = "根据id删除企业信息")
    public Result<String> deleteEnterprise(Long id) {
        if (id == null) {
            return Result.error("企业ID不能为空");
        }
        boolean result = enterpriseService.removeById(id) ;
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }
    //查询所有企业列表
    @GetMapping("/getAllEnterprise")
    @Operation(summary = "查询所有企业列表")

    public Result<List<Enterprise>> getAllEnterprise() {
        return Result.success("查询成功",enterpriseService.list());
    }

}
